//
//  NSDate+Category.swift
//  swift-微博
//
//  Created by chenrin on 2016/12/29.
//  Copyright © 2016年 zhuoheng. All rights reserved.
//

import UIKit

extension Date{
    static func dateWithStr(time:String) -> Date{
        //1.将服务器返回给我买的时间字符串转化为NSDate
        //1.1创建formatter
        let formatter  = DateFormatter()
        //1.2设置时间的格式
        formatter.dateFormat = "EEE MMM d HH:mm:ss Z yyyy"
        //1.3设置时区
        formatter.locale = Locale(identifier: "en")
        //1.4字符串转化
        let createDate = formatter.date(from: time)
        
        return createDate!
    }
    
    var descDate : String{
        let calendar = Calendar.current
        //1.判断是否是今天
        if calendar.isDateInToday(self as Date) {
            //1.0获取当前时间和系统时间的差距（秒数）
            let since = Int(Date().timeIntervalSince(self as Date))
            print(since)
            //1.1判断是否是刚刚
            if since < 60 {
                return "刚刚"
            }else if since < 60 * 60{
                return "\(since / 60)分钟前"
            }else{
                return "\(since / 60 / 60)小时前"
            }
        }
        //2.判断是否是昨天
        var formatterStr = "HH:mm"
        if calendar.isDateInYesterday(self as Date) {
            formatterStr = "昨天:" + formatterStr
            
        }
        else{
            //一年内
            formatterStr = "MM-dd" + formatterStr
            //更早的年
            
        }
        
        
        let formatter  = DateFormatter()
        
        formatter.dateFormat = formatterStr
        
        formatter.locale = Locale(identifier: "en")
        
        return formatter.string(from: self as Date)
    }
    
}
